<?php defined('SYSPATH') or die('No direct script access.');

/**
 * User_Temp_Model Class
 * 
 * Handles all database requests regarding the ae_user_temp table.
 * 
 * -----------------------------------------------------------------------------
 * Copyright (C) 2008   Patrick Clark (pubblichi.com/blog)
 * -----------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ------------------------------------------------------------------------------
 * @package     AuthorEyes
 * @subpackage  Models
 * @category    Authentication
 * @author      Patrick Clark (Tap)
 * @copyright   Copyright (c) 2008, pubblichi.com
 * @license     http://www.gnu.org/licenses/lgpl.html
 * @link        http://www.pubblichi.com/authoreyes
 * @info        This is an adaptation of the FreakAuth Light authentication library,
 * @info        written specifically for Kohana.
 * @info     	FreakAuth Light written by Daniel Vecchiato (danfreak) & Christophe Gragnic (grahack)
 * @info        Visit www.4webby.com/freakauth to download FreakAuth Light for CodeIgniter
 * @version     0.1
 *
 */
class User_Temp_Model extends Model {
	/*-------------+
	|  PROPERTIES  |
	+-------------*/
	// Table prefix.
	protected $prefix = '';
	// Table name.
	protected $table;
	
	/*----------+
	|  METHODS  |
	+----------*/
	/**
	 * Initializes the class.
	 * 
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		// AuthorEyes table prefix.
		$this->prefix = Config::item('authoreyes.ae_table_prefix');
		$this->table = $this->prefix.'user';
	}
	
	/**
	 * Retrieves all records and all fields (or those passed in the $fields argument) from the
	 * table ae_user_temp.  It is [optional] to pass the wanted fields, query limit, and where clause.
	 * 
	 * @param   string Field names to be returned ($fields).
	 * @param   array  Record limit ($limit).
	 * @param   string Where clause ($where).
	 * @return  mixed
	 */
	public function get_user_temp($fields = NULL, $limit = NULL, $where = NULL)
	{
		// Building query...
		($fields != NULL) ? $this->db->select($fields) : '';
		($where != NULL) ? $this->db->where($where) : '';
		($limit != NULL) ? $this->db->limit($limit['number'], $limit['offset']) : '';
		
		// Assigns a query object.
		$query = $this->db->get($this->table);
		
		// Check to see if the result exists and returns result object, otherwise FALSE.
		if ($query->count() > 0)
		{
			return $query;
		}
		else
		{
			return FALSE;
		}
	}
	
	/**
	 * Retrieves all records and all fields (or those passed in the $fields argument) from the
	 * table ae_user_temp.  It is [optional] to pass the wanted fields and the where clause.
	 * 
	 * @param   string Field names to be returned ($fields).
	 * @param   string Where clause ($where).
	 * @return  mixed
	 */
	public function get_user_temp_where()
	{
		// Building query...
		($fields != NULL) ? $this->db->select($fields) : '';
		($where != NULL) ? $this->db->where($where) : '';
		
		// Assigns a query object.
		$query = $this->db->get($this->table);
		
		// Check to see if the result exists and returns result object, otherwise FALSE.
		if ($query->count() > 0)
		{
			return $query;
		}
		else
		{
			return FALSE;
		}
	}
	
	/**
	 * Insert values in ae_user_temp table after the first registration wizard.
	 * 
	 * @param  array Values for temporary user registration ($values).
	 * @return void
	 * @todo   Ensure this insert can take an array, otherwise we're going to have to loop it somehow.
	 */
	public function insert_user_for_registration()
	{
		// Insert query.
		$this->db->insert($this->table, $values);
	}
	
	/**
	 * Queries the ae_user_temp table to get user data (login and password).
	 * 
	 * @param  varchar Username ($username).
	 * @param  varchar Password ($password).
	 * @return void
	 */
	public function get_user_login_data($username, $password)
	{
		// Building query...
		$this->db->where("'user_name' = $username AND 'password' = $password");
		
		// Assigns a query object.
		$query = $this->db->get($this->table);
		
		// Check to see if the result exists and returns result object, otherwise FALSE.
		if ($query->count() > 0)
		{
			return $query;
		}
		else
		{
			return FALSE;
		}
	}
	
	/**
	 * Find the user that needs to be activated after he/she clicked the activation
	 * link in the e-mail.
	 * 
	 * @param  int     User ID ($id).
	 * @param  varchar Activation Code ($activation_code).
	 * @return mixed
	 */
	public function get_user_for_activation($id, $activation_code)
	{
		// Building query...
		$this->db->where("'id' = $id AND 'activation_code' = $activation_code");
		
		// Assigns a query object.
		$query = $this->db->get($this->table);
		
		// Check to see if the result exists and returns result object, otherwise FALSE.
		if ($query->count() > 0)
		{
			return $query;
		}
		else
		{
			return FALSE;
		}
	}
	
	/**
	 * Deletes the ae_user_temp record.
	 * 
	 * It is used after the contents of the ae_user_temp table are transferred
	 * to the ae_user table.
	 * 
	 * @param  int User ID ($id).
	 * @return void
	 */
	public function delete_user_after_activation()
	{
		$this->db->where('id', $id);
		$this->db->delete($this->table);
	}
	
	/**
	 * Needed by the library AuthorEyes function clean_expired_user_temp()
	 * 
	 * @return Query object
	 */
	public function get_user_temp_created()
	{
		return $this->db->query('SELECT id AS id, UNIX_TIMESTAMP(created) AS created FROM '.$this->table);
	}
}
?>
